package gulimall.member.controller;

import java.util.Arrays;
import java.util.Map;

import gulimall.exception.BizCodeEnum;
import gulimall.member.bo.MemberLoginBO;
import gulimall.member.bo.MemberRegistBO;
import gulimall.member.bo.SocialUserBO;
import gulimall.member.exception.PhoneExistException;
import gulimall.member.exception.UsernameExistException;
import gulimall.vo.MemberRespVO;
import org.springframework.web.bind.annotation.*;

import gulimall.member.entity.MemberEntity;
import gulimall.member.service.MemberService;
import gulimall.utils.PageUtils;
import gulimall.utils.R;

import javax.annotation.Resource;


/**
 * 会员
 *
 * @author HLF
 * @email 1132814637@qq.com
 * @date 2022-05-09 08:00:24
 */
@RestController
@RequestMapping("/member/member")
public class MemberController {
    @Resource
    private MemberService memberService;

    /**
     * 会员注册
     */
    @PostMapping("/regist")
    public R regist(@RequestBody MemberRegistBO memberRegistBO) {

        try {
            memberService.regist(memberRegistBO);
        } catch (PhoneExistException e) {
            // 返回对应的异常信息
            return R.error(BizCodeEnum.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnum.PHONE_EXIST_EXCEPTION.getMessage());
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnum.USER_EXIST_EXCEPTION.getCode(), BizCodeEnum.USER_EXIST_EXCEPTION.getMessage());
        }

        return R.ok();
    }

    /**
     * 会员网站账号登录
     */
    @PostMapping("/login")
    public R login(@RequestBody MemberLoginBO memberLoginBO) {
        MemberRespVO memberRespVO = memberService.memberLogin(memberLoginBO);
        return memberRespVO != null
                ? R.ok().put("member", memberRespVO)
                : R.error(
                BizCodeEnum.MEMBER_LOGIN_EXCEPTION.getCode(),
                BizCodeEnum.MEMBER_LOGIN_EXCEPTION.getMessage());
    }

    /**
     * 会员社交登录
     */
    @PostMapping("/oauth2/login")
    public R oauth2Login(@RequestBody SocialUserBO socialUserBO) {
        MemberEntity memberEntity = memberService.memberOauth2Login(socialUserBO);
        return R.ok().put("memberEntity", memberEntity);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("member:member:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("member:member:info")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("member:member:save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("member:member:update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("member:member:delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
